001    /* $RCSfile: DESede2KeySecretKeyGeneratorEngine.java,v $
002     * $Revision: 1.12 $
003     * $Date: 2003/10/04 19:18:38 $
004     * $Author: uwe_guenther $
005     * $State: Exp $
006     *
007     * Created on August 13, 2001 2:51 PM
008     *
009     * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
010     *
011     * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
012     * ServiceProvider is a library, written in JavaTM, that should be 
013     * used in HBCI banking applications (clients and may be servers),
014     * to do cryptographic operations.
015     *
016     * The jhbci library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     *
021     * The jhbci library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     *
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *
030     */
031    
032    package de.cscc.crypto.provider;
033    
034    import java.security.InvalidAlgorithmParameterException;
035    import java.security.InvalidParameterException;
036    import java.security.SecureRandom;
037    import java.security.spec.AlgorithmParameterSpec;
038    
039    import javax.crypto.KeyGeneratorSpi;
040    import javax.crypto.SecretKey;
041    
042    /** 
043     * DESede2KeySecretKeyGeneratorEngine Class provides the functionality
044     * of a DESede key generator for two keys (means 112 or 128 bit key length).
045     * This key generator generates only keys for the "DESede2Key" algorithm in
046     * the JHBCI crypto provider.
047     *
048     * This KeyGenerator Object is re-useable, i.e., after a key has been generated,
049     * the same KeyGenerator Object  can be re-used to generate further keys.
050     *
051     * <pre>
052     *
053     * Use the DESede2KeySecretKeyGeneratorEngine through the JCE in the following 
054     * way:
055     *
056     * import javax.crypto.KeyGenerator;
057     * import javax.crypto.SecretKey;
058     * import java.security.SecureRandom;
059     *
060     * KeyGenerator kg = new KeyGenerator.getInstance("DESede2Key", "JHBCI");
061     * kg.init(new SecureRandom());
062     * SecretKey key = kg.generateKey();
063     *
064     * //Use key further in a DESede2Key Cipher from the JHBCI provider.
065     * //Or do some other useful things with this nicely generated SecretKey.
066     *
067     * </pre>
068     *
069     *
070     * In this case you does not explicitly initialize the KeyGenerator Object
071     * (in our example code that means <code>kg.init(new SecureRandom());</code>)
072     * we initialize the KeyGenerator Object with <code>new SecureRandom()</code>.
073     * So you can omit the line <code>kg.init(new SecureRandom());</code>.
074     * With this code we will get a SecureRandom implementation of the
075     * higest-priority installed provider. If no one of the installed
076     * providers supply an implementation of SecureRandom, a system provided
077     * source of randomness will be used.
078     * <pre>
079     *
080     * See the changed example:
081     *
082     * import javax.crypto.KeyGenerator;
083     * import javax.crypto.SecretKey;
084     * import java.security.SecureRandom;
085     *
086     * KeyGenerator kg = new KeyGenerator.getInstance("DESede2Key", "JHBCI");
087     * SecretKey key = kg.generateKey();
088     *
089     * //Use key further in a DESede2Key Cipher from the JHBCI provider.
090     * //Or do some other useful things with this nicely generated SecretKey.
091     *
092     * </PRE>
093     * @author  <a href=mailto:uwe@cscc.de>Uwe Günther</a>
094     * @version $Revision: 1.12 $
095     */
096    public final class DESede2KeySecretKeyGeneratorEngine extends KeyGeneratorSpi {
097    
098        /** The delegate for this wrapper object */
099        private DESede2KeySecretKeyGeneratorImpl generator = 
100        new DESede2KeySecretKeyGeneratorImpl();
101        
102        /** 
103         * Creates new DESede2KeySecretKeyGeneratorEngine.
104         * Mostly used of the static function
105         * <code>KeyGenerator.getInstance(...)</code>.
106         *
107         * @throws SecurityException if the provider self integrity check fails. 
108         */
109        public DESede2KeySecretKeyGeneratorEngine() {
110            if (JHBCI.selfIntegrityChecking() == false) {
111                throw new SecurityException("JHBCI-Provider is tampered.");
112            }          
113        }
114    
115        /** 
116         * Returns a string representation of the object. 
117         *
118         * @return a string representation of the object.
119         */
120        public String toString() {
121            return this.generator.toString();
122        }
123        
124        /** 
125         * Initializes the key generator.
126         *
127         * @param random the source of randomness for this generator.
128         */
129        protected void engineInit(SecureRandom random) {
130            this.generator.init(random);
131        }
132        
133        /** 
134         * Initializes this key generator for a certain keysize, using the given
135         * source of randomness. We support only 112 bit and 128 bit keysize,
136         * which means the refers to the same result. 
137         *
138         * @param keysize the keysize. This is an algorithm-specific metric,
139         * specified in number of bits.
140         * @param random the source of randomness for this key generator.
141         * @throws InvalidParameterException if keysize is wrong or not supported.
142         */
143        protected void engineInit(int keysize, SecureRandom random) 
144                throws InvalidParameterException {
145            this.generator.init(keysize, random);
146        }
147        
148        /** 
149         * Initializes the key generator with the specified parameter
150         * set and a user-provided source of randomness. The JHBCI
151         * key generator implementation don't use any AlgorithmParameterSpec's.
152         * This means if you want to init the KeyGenerator Object with this
153         * method <code>params</code> have to be null or
154         * InvalidAlgorithmParameterException will be thrown.
155         *
156         * @param params the key generation parameters.
157         * @param random the source of randomness for this key generator.
158         * @throws InvalidAlgorithmParameterException if <code>params</code> is
159         * inappropriate for this key generator. We only support <code>null</code> 
160         * as parameter for <code>params</code>.
161         */
162        protected void engineInit(AlgorithmParameterSpec params, SecureRandom random) 
163                throws InvalidAlgorithmParameterException {
164            this.generator.init(params, random);
165        }
166        
167        /** 
168         * Generates a secret key. This method can re-used more than once
169         * for the KeyGenerator Object to generate a lot of SecretKey's.
170         *
171         * @return the new key.
172         */
173        protected SecretKey engineGenerateKey() {
174            return this.generator.generateKey();
175        }
176    }